Imports System.Text.RegularExpressions
Imports System.IO
Imports System.Xml

Module modXMLFileRead

  Public Const DDL_TEMPLATE_FILE As String = "DDLTemplate.xml"
  Public Const CATEGORY_FILE As String = "DatabaseCategoryMap.xml"
  Public Const CODE_TEMPLATE_FILE As String = "CodeTemplate.xml"
  Public Const CONFIG_FILE As String = "config.xml"

  Public Const CLIENT_INDC_BE As String = "BE"
  Public Const CLIENT_INDC_FE As String = "FE"
  Public Const CLIENT_INDC_CATEGORY As String = "Category"

  Public Function mFnReadValue(ByVal aStrApplicationName As String, _
                  ByVal aStrKeyName As String, _
                  ByVal aStrDefault As String, _
                  ByRef aStrReturnedString As String, _
                  ByVal aStrFileName As String) As Boolean
    Dim lObjXMLFile As New XmlDocument
    lObjXMLFile.Load(aStrFileName)

    Try
      If aStrKeyName.Trim = String.Empty Then
        If lObjXMLFile.DocumentElement.Item(aStrApplicationName).HasAttributes = True Then
          Dim lObjOpenFile As New OpenFileDialog
          Dim lObjFile As IO.StreamReader
          Dim lStrTmplfilePath As String
          If UCase(lObjXMLFile.DocumentElement.Item(aStrApplicationName).GetAttribute("DefaultTemplate")) = "FALSE" Then
            With lObjOpenFile
              .DefaultExt = "tmpl"
              .Filter = "Template File|*.tmpl"
              .Title = "Select " & aStrApplicationName & " Template File"
              If .ShowDialog = DialogResult.Cancel Then Return False
              lStrTmplfilePath = .FileName

            End With
          Else
            lStrTmplfilePath = lObjXMLFile.DocumentElement.Item(aStrApplicationName).InnerText
          End If
          Try
            lObjFile = New IO.StreamReader(lStrTmplfilePath)
            aStrReturnedString = lObjFile.ReadToEnd()
            lObjFile.Close()
          Catch ex As Exception
            If IsNothing(lObjFile) = False Then
              lObjFile.Dispose()
              lObjFile = Nothing
            End If
          End Try
        Else
          If aStrFileName.Contains(CODE_TEMPLATE_FILE) Then
            aStrReturnedString = Application.StartupPath & "\" & lObjXMLFile.DocumentElement.Item(aStrApplicationName).InnerText
          Else
            aStrReturnedString = lObjXMLFile.DocumentElement.Item(aStrApplicationName).InnerText
          End If
        End If
      Else
        aStrReturnedString = lObjXMLFile.DocumentElement.Item(aStrApplicationName).Item(aStrKeyName).InnerText
      End If
      If aStrReturnedString.Trim = String.Empty Then aStrReturnedString = aStrDefault
      aStrReturnedString = Regex.Replace(aStrReturnedString, REGX_COMMENT_WORD, String.Empty)
      Return True
    Catch ex As Exception
      Return False
    End Try

  End Function

  Public Function mFnReadChildNodes(ByVal aStrApplicationName As String, _
                                    ByVal aStrFileName As String) As String
    Dim lObjXMLFile As New XmlDocument
    Dim lStrRtn As String = String.Empty

    lObjXMLFile.Load(aStrFileName)
    Try
      For Each lObjNode As Xml.XmlNode In lObjXMLFile.DocumentElement.Item(aStrApplicationName).ChildNodes
        If lObjNode.NodeType = XmlNodeType.Element Then _
          lStrRtn = lStrRtn & lObjNode.Name & " "
      Next
      Return lStrRtn.Trim
    Catch ex As Exception
      Return lStrRtn
    End Try
  End Function

  Public Function mFnReadAttribute(ByVal aStrApplicationName As String, _
                                   ByVal aStrElement As String, _
                                   ByVal aStrAttribute As String, _
                                   ByVal aStrFileName As String) As String
    Dim lObjXMLFile As New XmlDocument
    lObjXMLFile.Load(aStrFileName)
    Try
      Return lObjXMLFile.DocumentElement.Item(aStrApplicationName).Item(aStrElement).GetAttribute(aStrAttribute)
    Catch ex As Exception
      Return String.Empty
    End Try
  End Function

  Public Function mFnReadElements(ByVal aStrApplicationName As String, _
                                  ByVal aStrFileName As String) As ArrayList
    Dim lObjXMLFile As New XmlDocument
    Dim lArrRtn As New ArrayList
    lObjXMLFile.Load(aStrFileName)
    Try
      For Each lObjXMLNode As XmlNode In lObjXMLFile.DocumentElement.Item(aStrApplicationName).ChildNodes
        If lObjXMLNode.NodeType = XmlNodeType.Element Then _
          lArrRtn.Add(lObjXMLNode.Name)
      Next
      Return lArrRtn
    Catch ex As Exception
      Return lArrRtn
    End Try
  End Function

  Public Function mFnReadElementFromValue(ByVal aStrApplicationName As String, _
                                  ByVal aStrValue As String, _
                                  ByVal aStrFileName As String) As String
    Dim lObjXMLFile As New XmlDocument
    Dim lStrRtn As String = String.Empty
    lObjXMLFile.Load(aStrFileName)
    Try
      For Each lObjXMLNode As XmlNode In lObjXMLFile.DocumentElement.Item(aStrApplicationName).ChildNodes
        If lObjXMLNode.NodeType = XmlNodeType.Element Then
          If lObjXMLNode.InnerText.Trim = aStrValue.Trim Then
            lStrRtn = lObjXMLNode.Name
            Exit For
          End If
        End If
      Next
      Return lStrRtn
    Catch ex As Exception
      Return lStrRtn
    End Try
  End Function

End Module

